<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改密码</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/style/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/style/layui/layui.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/style/jquery-2.1.0.js"></script>
</head>

<body>
<div class="layui-card">
    <div class="layui-card-header">修改密码</div>
    <div class="layui-card-body">
        <form class="layui-form" id="updateForm">
            <input type="text" style="display: none" name="staffId" value="${name}">
            <div class="layui-form-item">
                <label class="layui-form-label">当前密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="oldPassword" required lay-verify="required|oldPassword" placeholder="请输入旧密码"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">新密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="password" id="newPassword" required lay-verify="required|newPassword1|newPassword" placeholder="请输入新密码"
                           class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">6到12个字符</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">确认新密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="password" required lay-verify="required|newPassword2" placeholder="请确认新密码"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="reset" class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>
</body>

<script>

    //Demo
    layui.use('form', function () {
        var form = layui.form;
        //修改自己密码表单验证和提交
        <%--var staffPassword = ${nowStaff.password};--%>
        var staffPassword = '${pwd}';
        form.verify({
            oldPassword: function (value) { //value：表单的值、item：表单的DOM对象

                if (staffPassword != value) {
                    return '旧密码不正确';
                }
            }
            ,newPassword1: function (value) { //value：表单的值、item：表单的DOM对象
                if (staffPassword == value) {
                    return '不能和更改前密码一致';
                }
            }
            , newPassword: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ]
            , newPassword2: function (value, item) { //value：表单的值、item：表单的DOM对象
                if ($("#newPassword").val() != value) {
                    return '两次输入密码不一致';
                }
            }
        });
        //监听提交
        form.on('submit(formDemo)', function (data) {
           // layer.msg(JSON.stringify(data.field));
             $.ajax({
                 url: '${pageContext.request.contextPath}/login?state=update',
                 type: 'post',
                 async: false,
                 data: data.field,
                 success: function (result) {
                     if (result != 0) {
                         layer.msg("修改成功,请重新登录!");
                         // $("#updateForm")[0].reset();
                         // toastr.success('修改密码成功');
                         setTimeout(function () {
                             window.location.href="${pageContext.request.contextPath}/login?state=loginOut";
                             window.parent.location.reload();
                         },2500);
                     } else {
                         // toastr.error('修改密码失败');
                         layer.msg("修改失败！");
                     }
                     <%--window.location.href = "${pageContext.request.contextPath}/view/login/login.jsp";--%>
                 },
             });
            return false;

        });
    });
</script>
</html>
